package de.slothsoft.bookmaker.ui;

import de.slothsoft.bookmaker.ProgressMonitor;
import javafx.application.Platform;
import javafx.scene.control.Labeled;
import javafx.scene.control.ProgressIndicator;

public class ProgressIndicatorMonitor implements ProgressMonitor {

    protected final Labeled textField;
    protected final ProgressIndicator progressIndicator;

    protected double progress;
    protected double step = 1;

    public ProgressIndicatorMonitor(Labeled textField, ProgressIndicator progressIndicator) {
        this.textField = textField;
        this.progressIndicator = progressIndicator;
    }

    @Override
    public void incrementValue() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                ProgressIndicatorMonitor.this.progress += ProgressIndicatorMonitor.this.step;
                ProgressIndicatorMonitor.this.progressIndicator.setProgress(ProgressIndicatorMonitor.this.progress);
            }
        });
    }

    @Override
    public void setText(final String text) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                ProgressIndicatorMonitor.this.textField.setText(text);
            }
        });
    }

    @Override
    public void clear() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                ProgressIndicatorMonitor.this.progress = 0;
                ProgressIndicatorMonitor.this.progressIndicator.setProgress(0);
                ProgressIndicatorMonitor.this.textField.setText("");
            }
        });
    }

    public double getStep() {
        return this.step;
    }

    @Override
    public void setStep(double step) {
        this.step = step;
    }

}
